local ss = "ma"

use "$temp/student_level_`ss'_w_unshrunken_VA_schoolMeans", clear
local student_cov2 i.grade i.ethnic i.female i.gifted i.disability i.MIGRANT i.ENG_LEARN  i.DISADV_persist read_accom math_accom  *_miss i.year#i.grade age

cap drop lea schlcode

		bys schlcode_ma grade year section_ma: egen mean_ma_score_class = mean(l_ma)
		bys schlcode_rd grade year section_rd: egen mean_rd_score_class = mean(l_rd)
		
		foreach sss in ma rd {
			forval g=4/8 {
				gen clsmn_`sss'_1_g`g' = mean_`sss'_score_class^1*(grade==`g')
				gen clsmn_`sss'_2_g`g' = mean_`sss'_score_class^2*(grade==`g')
				gen clsmn_`sss'_3_g`g' = mean_`sss'_score_class^3*(grade==`g')
				
				}
		}


*** vam from CFR 2014*/
cap gen j = teachid_`ss' // teacher index
cap gen t = year // year

vam  score_`ss' , controls(IL* `student_cov2' numstudents_`ss' class_mn_*_`ss' clsmn_*g*) teacher(j) year(t) class(section_`ss') tfx_resid(teachid_`ss') output("$temp/cfr_va_classmeans_`ss'")

use "$basedata/va_homogeneous_estimates_drift", clear
merge m:1 j t  using "$temp/cfr_va_classmeans_ma"
drop if _m!=3
drop _m
ren tv mu_homog_ma_cfr
save "$basedata/va_homogeneous_estimates_drift_classmeans_cfr", replace
